package org.soenea.doitall.domain.command;

import java.util.List;
import org.dsrg.soenea.domain.command.CommandError;
import org.dsrg.soenea.domain.command.CommandException;
import org.dsrg.soenea.domain.command.impl.Command;
import org.dsrg.soenea.domain.helper.Helper;
import org.soenea.doitall.domain.model.group.*;
import org.soenea.doitall.domain.model.group.mapper.*;
import org.soenea.doitall.domain.model.user.*;
import org.soenea.doitall.domain.model.user.mapper.*;

public class ViewGroupCommand extends Command {
	public ViewGroupCommand(Helper helper) {
		super(helper);
	}

	@Override
	public void setUp() throws CommandException {
		// TODO Auto-generated method stub		
	}

	@Override
	public void process() throws CommandException {
		Group group;
		long groupID = 0;
		long userID = 0;
		
		try {
			userID = Long.parseLong(helper.getSessionAttribute("UserID").toString());
			groupID = Long.parseLong(helper.getSessionAttribute("GroupID").toString());
			
			if (groupID == 0) {
				User user = UserInputMapper.find(userID);
				helper.setRequestAttribute("user", user);
				helper.setRequestAttribute("warning", "You are not part of a group.");
				throw new CommandException("You are not part of a group.");
			}
   
			helper.setRequestAttribute("self", userID);
			
			group = GroupInputMapper.find(groupID);
			helper.setRequestAttribute("group", group);
    		
    		List<User> users = UserInputMapper.findByGroupID(groupID);
    		helper.setRequestAttribute("users", users);
    	} catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
    	}
	}

	@Override
	public void tearDown() throws CommandError {
		// TODO Auto-generated method stub
	}
}
